package com.wsx.java.boddy.agent;

import com.wsx.java.boddy.agent.okhttp.OkHttpTransform;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;

import java.lang.instrument.Instrumentation;

/**
 * @author wangshuaixin
 * @Title: com.wsx.java.boddy.agent.CommonAgent
 * @Description: TODO
 * @date 2019/02/19
 */
public class CommonAgent {




    public static void premain(String agentArgs, Instrumentation inst) {

        String serviceName = agentArgs;

        AgentBuilder.Listener listener = new AgentBuilder.Listener() {
            @Override
            public void onDiscovery(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) {

            }

            @Override
            public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b, DynamicType dynamicType) {

            }

            @Override
            public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b) {

            }

            @Override
            public void onError(String s, ClassLoader classLoader, JavaModule javaModule, boolean b, Throwable throwable) {

            }

            @Override
            public void onComplete(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) {

            }
        };

        new AgentBuilder.Default()
                .type(ElementMatchers.named("okhttp3.internal.http.HttpEngine$NetworkInterceptorChain"))
                .transform(new OkHttpTransform("com.wsx.java.boddy.agent.okhttp.OkHttpInteceptor"))
                ///.type()
                ///.transform()
                .with(listener)
                .installOn(inst);

    }
}
